package com.portal.business.show.action;




import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.portal.business.show.ShowService;
import com.portal.business.show.action.model.ShowModel;
import com.portal.business.show.dto.ShowLogDTO;
import com.portal.framework.dao.ParameterCatch;
import com.portal.framework.webutil.BaseAction;


/**
 * 
 * 项目名称: Portal     
 * 类名称:   ShowAction     
 * 类描述:   在线演示(查询功能)  
 * 创建人:   骆海东
 * 创建时间: Aug 31, 2011 10:15:43 AM     
 * 最后修改人:   骆海东
 * 最后修改时间: Aug 31, 2011 10:15:43 AM     
 * 最后修改备注:
 */
public class ShowAction extends BaseAction {


	private static final long serialVersionUID = 3246141146559930877L;

	private ShowModel model = new ShowModel();
	
	private ShowService showService;
	
	
	/**
	 * 进入页面
	 * @return
	 */
	public String goShowMain() {
		return SUCCESS;
	}
	/**
	 * 查询
	 * @return
	 */
	public String doSearchAnswerMain() {
		String word=getRequest().getParameter("keyword");
		Map<String,String> parameterMap=ParameterCatch.parameterMap;
//		String url=parameterMap.get("robotAnswerUrl")+word;
		String url="http://127.0.0.1/mtrobot/dodemo.do?q="+word;
		ShowLogDTO showLogDTO=new ShowLogDTO();
		showLogDTO.setName(word);
		showLogDTO.setCreateDate(new Date());
		showLogDTO.setIp(getRemortIP(getRequest()));
		String retult="";
		try {
			retult=getRemoteHtml(url);
			showLogDTO.setAnswer(retult);
			showLogDTO.setStatus(0);
//			this.outputDateinfo(answer);
		} catch (IOException e) {
			showLogDTO.setAnswer("机器人无响应...");
			showLogDTO.setStatus(1);
			retult="1.访问超时,可能存在网络异常,检查后请重试...";
//			this.outputDateinfo("访问超时,可能存在网络异常,检查后请重试...");
			e.printStackTrace();
		}finally{
			showService.addShowLog(showLogDTO);
			this.outputDateinfo(retult);
		}
		return NONE;
	}
	
	public String getRemortIP(HttpServletRequest request) {
 
		   String ip="";
	        //@YHJ：匹配大小写，保证无论Nginx如何配置代理参数，系统都能正常获取代理IP
	        Enumeration enumeration = request.getHeaderNames();
	        while(enumeration.hasMoreElements()){
	            String paraName=(String)enumeration.nextElement();
	            if("X-Forwarded-For".equalsIgnoreCase(paraName)){
	               ip=request.getHeader(paraName);
	               break;
	            }
	        }
	            String localIP = "";
	            if ((ip == null) || (ip.length() == 0) || (ip.equalsIgnoreCase(localIP)) || "unknown".equalsIgnoreCase(ip)) {
	                    ip = request.getHeader("Proxy-Client-IP");
	            }
	            if ((ip == null) || (ip.length() == 0) || (ip.equalsIgnoreCase(localIP)) || "unknown".equalsIgnoreCase(ip)) {
	                    ip = request.getHeader("WL-Proxy-Client-IP");
	            }
	            if ((ip == null) || (ip.length() == 0) || (ip.equalsIgnoreCase(localIP)) || "unknown".equalsIgnoreCase(ip)) {
	                    ip = request.getRemoteAddr();
	            }
	            return ip;

		} 


	/**
	 * 获取答案数据
	 * @param remoteFile
	 * @return
	 * @throws IOException
	 */
	public   String  getRemoteHtml(String remoteFile) throws IOException{
   	 	URL url = null;
		HttpURLConnection urlc = null;
		String sCurrentLine="";  
		java.io.BufferedReader l_reader=null;
		StringBuffer sTotalString = new StringBuffer("");  
		try {
			remoteFile=remoteFile.replaceAll("\\s", "%20");//转换空格
//			System.out.println("search ulr:"+remoteFile);
			url = new URL(remoteFile);
			urlc = (HttpURLConnection) url.openConnection();
			urlc.setRequestProperty("Accept-Language", "zh-cn");
//			urlc.setRequestProperty("User-Agent","Mozilla/6.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
			// 输入流    
		    l_reader = new java.io.BufferedReader(new java.io.InputStreamReader(urlc.getInputStream(),"UTF-8"));  

			 //得到返回的信息
			 while ((sCurrentLine = l_reader.readLine()) != null) {  
			  sTotalString.append(sCurrentLine); 
			  sTotalString.append("\n");
			 }
		}finally{
			if(l_reader!=null)
			l_reader.close();
			if(urlc!=null)
			urlc.disconnect();
		}
		return sTotalString.toString();
   }


	public ShowModel getModel() {
		return model;
	}



	public void setModel(ShowModel model) {
		this.model = model;
	}
	public ShowService getShowService() {
		return showService;
	}
	public void setShowService(ShowService showService) {
		this.showService = showService;
	}



}
